"use strict";
/* Controllers */
app.controller("costReportDetailController", ['$scope', '$location', '$rootScope', '$routeParams', 'costReportDetailService', function ($scope, $location, $rootScope, $routeParams, costReportDetailService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    vm.p.ownerId = $routeParams.ownerId;
    vm.p.period = $routeParams.period;
    vm.p.dt = $routeParams.dt;
    vm.p.basicPath = $routeParams.basicPath;

    if (vm.p.basicPath != null && typeof (vm.p.basicPath) != "undefined" && vm.p.basicPath.indexOf('|') >= 0) {
        vm.p.basicPath = vm.p.basicPath.replaceAll('|', '/')
    } else {
        vm.p.basicPath = ''
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.loadCostReportDetail = function () {
        $scope.listCostTableReportDetail();
    };


    $scope.listCostTableReportDetail = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = vm.p;

        if(vm.p.ownerId=='0') {
            params.ownerId = null;
        }
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        costReportDetailService.listCostTableReportDetail(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });

        $resultTable.bootstrapTable('hideColumn', 'basicPath');
        $scope.listCostTableReportDetail();
    };

    $scope.initializeController();

}]);

function toPencent(num, row, index) {
    if (num == null) {
        return "-";
    }
    return (parseFloat(num) * 100).toFixed(2) + '%'
}


/* Services */
app.service('costReportDetailService', ['ajaxService', function (ajaxService) {

    this.listCostTableReportDetail = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/cost_report/listCostTableReportDetail.do", successFunction, errorFunction);
    };
}]);


function costReportDetailIDFmt(value, row, index) {
    if(value == null) {
        return '-';
    }
    return '<a href="https://dp.58corp.com/data-map/detail-page/'+value+'" target="_blank"> '+value+' </a>';
}

function lifecycleFmt(value, row, index) {
    if (value == null) {
        return null;
    }
    var ret = '';
    if (value == 0) {
        ret = '-';
    } else if (value == 1) {
        ret = '1天';
    } else if (value == 3) {
        ret = '3天';
    } else if (value == 5) {
        ret = '5天';
    } else if (value == 7) {
        ret = '7天';
    } else if (value == 15) {
        ret = '15天';
    } else if (value == 33) {
        ret = '33天';
    } else if (value == 93) {
        ret = '93天';
    } else if (value == 183) {
        ret = '183天';
    } else if (value == 368) {
        ret = '368天';
    } else if (value == 730) {
        ret = '2年';
    } else if (value == 1095) {
        ret = '3年';
    } else if (value == 1825) {
        ret = '5年';
    } else if (value == 99999) {
        ret = '永久';
    } else {
        ret = formatColorCtx('未知:' + value, 'red');
    }
    return ret;
}



function isPartitionFmt(value, row, index) {
    if (value == null) {
        return null;
    }
    if (value == 1) {
        ret = '是';
    } else if (value == 0) {
        ret = '否';
    } else {
        ret = formatColorCtx('未知:' + value, 'red');
    }
    return ret;
}

function maxAccessSpanIn100DaysFmt(value, row, index) {
    if (value == null || value=='-') {
        return '-';
    }
    return value+'天';
}

